﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuoiThoProject.DAL;

namespace TuoiThoProject.BLL
{
    public class SystemMessageBLL
    {
        SystemMessageDataContext data = new SystemMessageDataContext();

        public List<tblMessage> getMessage_All()
        {
            return (from m in data.tblMessages
                    orderby m.dCreatedDate descending
                    select m).ToList();
        }

        public List<tblMessage> getMessage_isRead(bool isRead)
        {
            return (from m in data.tblMessages
                    orderby m.dCreatedDate descending
                    where m.isRead == isRead
                    select m).ToList();
        }

        public List<tblMessage> getMessage_fromUser(Guid fromUser)
        {
            return (from m in data.tblMessages
                    orderby m.dCreatedDate descending
                    where m.FromUser == fromUser
                    select m).ToList();
        }

        public List<tblMessage> getMessage_toUser(Guid toUser)
        {
            return (from m in data.tblMessages
                    orderby m.dCreatedDate descending
                    where m.FromUser == toUser
                    select m).ToList();
        }

        public void SendMessage(Guid fromUser, Guid toUser, string Subject, string Content)
        {
            tblMessage m = new tblMessage();
            m.FromUser = fromUser;
            m.ToUser = toUser;
            m.cSubject = Subject;
            m.cContent = Content;

            data.tblMessages.InsertOnSubmit(m);
            data.SubmitChanges();
        }

        public void DeleteMessage(int MessageId)
        {
            tblMessage m = data.tblMessages.SingleOrDefault(m1 => m1.nMessageId == MessageId);
            data.tblMessages.DeleteOnSubmit(m);
            data.SubmitChanges();
        }
    }
}